package com.world.business.dict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.dict.domain.DictData;
import com.world.business.dict.domain.query.DictDataAddRequest;
import com.world.business.dict.domain.vo.DictDataVO;
import com.world.business.dict.mapper.DictDataMapper;
import com.world.business.dict.service.IDictDataService;
import com.world.common.constants.RedisKeyConstants;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.security.SecurityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典数据 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-27
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<DictDataVO> getListByDictTypeId(Long id) {
        LambdaQueryWrapper<DictData> lambdaQuery = Wrappers.lambdaQuery(DictData.class);
        lambdaQuery.eq(DictData::getDelFlag, DelFlagEnum.NORMAL.getCode());
        lambdaQuery.eq(DictData::getDictTypeId, id);
        List<DictData> dictDataList = this.baseMapper.selectList(lambdaQuery);
        return BeanUtil.copyToList(dictDataList, DictDataVO.class);
    }

    @Override
    public DictDataVO get(Long id) {
        Object o = redisTemplate.boundHashOps(RedisKeyConstants.DICT_MAP_KEY).get(id.toString());
        if (BeanUtil.isEmpty(o)) {
            Map<String, DictDataVO> map = addRedisCache();
            o = map.get(id.toString());
            return (DictDataVO) o;
        }
        return (DictDataVO) o;
    }

    /**
     * 添加Redis缓存
     *
     * @return {@link Map}
     * @author WangYX
     * @date 2023/07/28 16:23
     */
    private Map<String, DictDataVO> addRedisCache() {
        LambdaQueryWrapper<DictData> lambdaQuery = Wrappers.lambdaQuery(DictData.class);
        lambdaQuery.eq(DictData::getDelFlag, DelFlagEnum.NORMAL.getCode());
        List<DictData> dictDataList = this.baseMapper.selectList(lambdaQuery);

        List<DictDataVO> dictDataVos = BeanUtil.copyToList(dictDataList, DictDataVO.class);
        Map<String, DictDataVO> map = dictDataVos.stream().collect(Collectors.toMap(i -> i.getId().toString(), Function.identity()));
        redisTemplate.boundHashOps(RedisKeyConstants.DICT_MAP_KEY).putAll(map);
        return map;
    }

    @Override
    public void delete(Long id) {
        //删除数据
        DictData build = DictData.builder().id(id).delFlag(DelFlagEnum.DELETE.getCode()).build();
        this.baseMapper.updateById(build);

        //清除Redis缓存
        redisTemplate.delete(RedisKeyConstants.DICT_MAP_KEY);
    }

    @Override
    public Long add(DictDataAddRequest request) {
        DictData build = DictData.builder()
                .dictTypeId(request.getDictTypeId())
                .label(request.getLabel())
                .value(request.getValue())
                .sort(request.getSort())
                .status(request.getStatus())
                .remark(request.getStatus())
                .delFlag(DelFlagEnum.NORMAL.getCode())
                .createBy(SecurityUtils.getLoginUser().getName())
                .createTime(LocalDateTime.now())
                .createById(SecurityUtils.getLoginUser().getId())
                .build();
        this.baseMapper.insert(build);

        //清除Redis缓存
        redisTemplate.delete(RedisKeyConstants.DICT_MAP_KEY);
        return build.getId();
    }

}
